package com.mtk.launcher.util;

import android.content.Context;
import android.text.TextUtils;

import com.mtk.launcher.R;
import com.mtk.launcher.power.bean.TimeDealEntity;
import com.mtk.launcher.power.bean.TimeEntity;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateUtil {


    /***
     * 获取当前的日期
     * @return
     */
    public static long getCurrentDateLong() {
        long current = System.currentTimeMillis();
        String currentDate = new SimpleDateFormat("yyyyMMdd").format(current);
        return Long.parseLong(currentDate);
    }


    public static String getShowFormatTime(String input) {
        if (TextUtils.isEmpty(input) || input.length() < 12) {
            return input;
        }
        try {
            String year = input.substring(0, 4);
            String month = input.substring(4, 6);
            String day = input.substring(6, 8);
            String hour = input.substring(8, 10);
            String minute = input.substring(10, 12);
            String second = input.substring(12, 14);
            String formatted = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
            return formatted;
        } catch (Exception e) {
            e.printStackTrace();
            return input;
        }
    }


    /***
     *
     * @param context
     * @param timeDealEntity
     * @param tag
     * 0 关机
     * 1 开机
     * @return
     */
    public static String getShowOnOffTime(Context context, TimeDealEntity timeDealEntity, int tag) {
        if (timeDealEntity == null) {
            return context.getString(R.string.no_setting_power);
        }
        String input = timeDealEntity.getOffTimeShow() + "";
        if (tag == 1) {
            input = timeDealEntity.getOnTimeShow() + "";
        }
        String year = input.substring(0, 4);
        String month = input.substring(4, 6);
        String day = input.substring(6, 8);
        String hour = input.substring(8, 10);
        String minute = input.substring(10, 12);
        String second = input.substring(12, 14);
        String formatted = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
        return formatted;
    }


    /***
     * 定时开关机用来解析时间的
     * @param timeLong
     * @return
     */
    public static TimeEntity getFormatLongTime(long timeLong) {
        String time = String.valueOf(timeLong);
        int year = Integer.parseInt(time.substring(0, 4));
        int month = Integer.parseInt(time.substring(4, 6));
        int day = Integer.parseInt(time.substring(6, 8));
        int hour = Integer.parseInt(time.substring(8, 10));
        int min = Integer.parseInt(time.substring(10, 12));
        TimeEntity entity = new TimeEntity(year, month, day, hour, min);
        return entity;
    }


    public static String formatCurrentDate() {
        long time = System.currentTimeMillis();
        return new SimpleDateFormat("yyyy-MM-dd ").format(Long.valueOf(time));
    }

    public static String formatCurrentTime() {
        long time = System.currentTimeMillis();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(Long.valueOf(time));
    }

    public static long getCurrentTimelONG() {
        long time = System.currentTimeMillis();
        String timeBack = new SimpleDateFormat("yyyyMMddHHmmss").format(Long.valueOf(time));
        long timeBackLong = Long.parseLong(timeBack);
        return timeBackLong;
    }

    /***
     * 将时间戳转换成时间数据
     * @param paramLong
     * @return
     */
    public static long formatBig(long paramLong) {
        return Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmss").format(Long.valueOf(paramLong)));
    }

    /**
     * 将long转化成时间戳
     *
     * @param time
     * @return
     */
    public static long StringToLongTimePower(String time) {
        long timeBack = -1;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            Date dateStart = format.parse(time);
            timeBack = dateStart.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeBack;
    }

}
